// pages/guidePage/index.js
const app = getApp()
const utils = require('../../../utils/util.js')
import Toast from '../../../vant-weapp/dist/toast/toast'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    tagTitles: [],
    tagsList: [],
    isChoose: false,
    id: null
  },

 async init() {
   const recommendedList = []
   const styleList = await  utils.$http(app.globalData.api.GetStyleList, {}, 'GET', true, 'JSON').then(res => {
    return res && res.data
   })
   const specialTopicsList = await utils.$http(app.globalData.api.GetSpecialTopics, {}, 'GET', true, 'JSON').then(res => {
     return res && res.data
   })
   const familyList = await utils.$http(app.globalData.api.GetFamilyNumbers, {}, 'GET', true, 'JSON').then(res => {
    return res && res.data
   })
   recommendedList.push({
      recommendedTitel: '风格关注',
      recommendedCode: 'TJ001',
      rkv: styleList
    })
    recommendedList.push({
      recommendedTitel: '热门话题',
      recommendedCode: 'TJ002',
      rkv: specialTopicsList
    })
    recommendedList.push({
      recommendedTitel: '家庭类型',
      recommendedCode: 'TJ003',
      rkv: familyList
    })
    this.setData({
     tagTitles: recommendedList
    })

    // 获取已选的兴趣标签
    const openId = wx.getStorageSync('openId')
    utils.$http(app.globalData.api.GetInterestList+ '/' + openId, 'GET', true, 'JSON').then(res => {
      if(res.success === 1 && res.data) {
        this.handlerSelectTag(res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setupInit()
    this.init()
  },
  
  handlerSelectTag:function(tags) {
      const styleIdList = tags.styleId.split(",")
      const specialIdList = tags.specialId.split(",")
      const familyIdList = tags.familyId.split(",")

      this.data.tagTitles.forEach(subItem => {
        if(subItem.recommendedCode === 'TJ001') {
          styleIdList.forEach(tag => {
            const [subTag] = subItem.rkv.filter(subTag => subTag.id === tag)
            if(subTag) {
             subTag.selected = true
            }
         })
        } else if(subItem.recommendedCode === 'TJ002') {
          if(specialIdList &&specialIdList.length) {
            specialIdList.forEach(tag => {
              const [subTag] = subItem.rkv.filter(subTag => subTag.id === tag)
              if(subTag) {
                subTag.selected = true
              }
          })
          }
        } else {
          familyIdList.forEach(tag => {
            const [subTag] = subItem.rkv.filter(subTag => subTag.id === tag)
            if(subTag) {
              subTag.selected = true
            }
          })
        }
      })

      this.setData({
          tagTitles: this.data.tagTitles,
          id: tags.id,
          tagsList: JSON.parse(JSON.stringify(this.data.tagTitles))
      })
  },

  goBack: function () {
    wx.navigateBack({

    })
  },
  goHome: function () {
    wx.switchTab({
      url: '/pages/home/main/index'
    })

  },

  setupInit: function () {
    utils.getFrameById('#navigation', (frame) => {
      // console.log('height:', frame.height)
      if (frame && frame.height) {
        this.setData({
          navHeight: frame.height
        })
      }
    })
  },

  tagItemClick: function (e) {
    let sectionIndex = e.currentTarget.dataset.sectionindex
    let tagindex = e.currentTarget.dataset.tagindex

    this.data.tagTitles[sectionIndex].rkv[tagindex].selected = !this.data.tagTitles[sectionIndex].rkv[tagindex].selected
    this.setData({
      tagTitles: this.data.tagTitles,
      tagsList: JSON.parse(JSON.stringify(this.data.tagTitles))
    })
  },

  skipStep: function () {
    wx.setStorageSync('showGuide', false)
    this.gotoMainPage()
  },

  chooseOkBtnClick: function () {
    let loop = this.data.tagsList.length
    this.data.tagsList.forEach((item, index) => {
      item.rkv = item.rkv.filter((item1, index) => {
        return item1.selected === true
      })
    })
    this.data.tagsList = this.data.tagsList.filter((item, index) => {
      return item.rkv.length > 0
    })
    let styleIdList = []
    let specialIdList = []
    let familyIdList = []
    
    this.data.tagsList.forEach(item => {
      if(item.recommendedCode === 'TJ001') {
        styleIdList = item.rkv
      } else if(item.recommendedCode === 'TJ002') {
        specialIdList = item.rkv
      } else  {
        familyIdList = item.rkv
      }
    })
    const styleId = styleIdList.map(item => item.id)
    const specialId = specialIdList.map(item => item.id)
    const familyId = familyIdList.map(item => item.id)
    
    // if(styleIdList.length + specialIdList.length + familyIdList.length < 3) {
    //   wx.showToast({ title: '至少选择3个感兴趣话题', icon: 'none' });
    //   return 
    // } 
    const post = {
      id: this.data.id,
      openId: wx.getStorageSync('openId'),
      styleId: styleId.join(','),
      specialId: specialId.join(','),
      familyId: familyId.join(','),
    }
    
    utils.$http(app.globalData.api.SaveRecommended , post, 'PUT', false, 'JSON').then(res => {
      this.goBack()
    })
  },

  gotoMainPage: function () {
    wx.switchTab({
      url: '/pages/home/main/index'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})